#Load Data
df<-data.frame( x= c(10,20,30,40,50,60,70,80,90),
                y= c(420,365,285,220,176,117,69,34,5))

#Scatter Plot
plot(df$x,df$y)

#Does a linear adjustment seem justified? What coefficient should you calculate with R?
#based on the scatter diagram, we can suspect a linear relationship between these two
#variables. The points in the scatter diagram roughly follow a straight line,
#suggesting that there is a linear trend between the variables x and y.

#Fit a model 
model<- lm(y~x,data=df)

#Get explanatory and explained variables
summary(model)
 
#Get Residuals
model$residuals

#Verify that the mean of residuals equal 0
mean(model$residuals)

#Error variance estimator"MSE"(s squared), s-squared = sse/n-2
 
  y_hat<- model$fitted.values
  
  #Calculate SSE
  SSE <- sum((df$y - y_hat)^ 2)
  SSE
  
  #Calculate s squared
  s_squared <- SSE / 9 - 2
  s_squared
 
#variance of b0 and b1
  
   x_bar <- mean(x)
 
   #for 𝛽0
   v_B0 <- s_squared * ( sum(x^2) / ( n * sum((x - x_bar )^2)) )
   v_B0
   
   #for 𝛽1
   v_B1 <- s_squared / ( sum((x - x_bar )^2) )
   v_B1
 
 
#Confidence interval:𝛽 +- t_critical. S𝛽
confint( model, level = .95)

#hypothesis for b0
  t_critical <- qt(df= n-2, p=0.025)
  abs(t_critical)
  
  summary <- summary(model)
  coef_table <- summary$coefficients
  
  #t for b0 (or from summary)
  b0_t_value <- coef_table[5]
  b0_t_value
  
#Anova
anova(model)

#R^2(SSR/SST)
R_squared <- ssr/sst
R_squared


 